import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/alert/alert.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

Future<int> showChangePriceAlert(
  BuildContext context, {
  String title,
  String hintText,
  String onConfirmText = '保存',
  String onCancelText = '取消',
  int initialValue,
  ValueChanged<dynamic> onConfirm,
}) {
  var formatPrice = AmountUtil.price2String(initialValue);
  String text = formatPrice;

  return showAlert<int>(
    context,
    content: _ChangePriceAlert(
      title: title,
      hintText: hintText,
      initialValue: formatPrice,
      onChanged: (value) {
        text = value;
      },
    ),
    onCancel: (context) {
      Navigator.pop(context);
    },
    onConfirm: (context) {
      if (!RegUtil.testPrice(text)) {
        toast('请输入正确的价格格式');
        return;
      }

      onConfirm(AmountUtil.string2Price(text));
    },
  );
}

class _ChangePriceAlert extends StatefulWidget {
  final String title;
  final String onConfirmText;
  final String onCancelText;
  final String hintText;
  final String initialValue;
  final ValueChanged<String> onChanged;

  const _ChangePriceAlert({
    Key key,
    this.title,
    this.onConfirmText,
    this.onCancelText,
    this.hintText,
    this.onChanged,
    this.initialValue,
  }) : super(key: key);

  @override
  __ChangePriceAlertState createState() => __ChangePriceAlertState();
}

class __ChangePriceAlertState extends State<_ChangePriceAlert> {
  OutlineInputBorder _inputBorder = OutlineInputBorder(
    borderSide: BorderSide(
      color: ColorConstant.e5e5,
      width: 1.px * 2,
    ),
    borderRadius: BorderRadius.circular(
      10.w,
    ),
  );

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(
        horizontal: 30.w,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(
              top: ScreenUtil.instance.setWidth(47.5),
              bottom: ScreenUtil.instance.setWidth(27.5),
            ),
            child: Text(
              widget.title,
              style: TextStyleConstant().normal_1_28,
            ),
          ),
          Stack(
            alignment: Alignment.center,
            children: <Widget>[
              TextFormField(
                keyboardAppearance: Brightness.light,
                initialValue: widget.initialValue,
                onChanged: widget.onChanged,
                decoration: InputDecoration(
                  enabledBorder: _inputBorder,
                  focusedBorder: _inputBorder,
                  hintText: widget.hintText,
                  hintStyle: TextStyleConstant().normal_4_28,
                  contentPadding: EdgeInsets.symmetric(
                    vertical: 22.w,
                    horizontal: 60.w,
                  ),
                ),
                style: TextStyleConstant().normal_1_28,
                keyboardType: TextInputType.numberWithOptions(decimal: true),
                inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.numberReg)],
              ),
              Positioned(
                top: 0,
                left: 0,
                bottom: 0,
                child: Container(
                  width: 60.w,
                  alignment: Alignment.center,
                  child: Text(
                    '¥',
                    style: TextStyle(
                      fontSize: 32.sp,
                      color: ColorConstant.ff33,
                      height: 1,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
